package system

import (
	"gin-sns/app/ginframe/response"
	"gin-sns/app/ginframe/utils/gconv"
	"gin-sns/app/model"
	deptModel "gin-sns/app/model/system/dept"
	"gin-sns/app/service/system"

	"net/http"

	"github.com/gin-gonic/gin"
)

var DeptController = newDeptController()

func newDeptController() *deptController {
	return &deptController{}
}

type deptController struct {
}

//列表页
func (c *deptController) List(ctx *gin.Context) {
	response.BuildTpl(ctx, "system/dept/list").WriteTpl()
}

//列表分页数据
func (c *deptController) ListAjax(ctx *gin.Context) {
	var req *deptModel.SelectPageReq
	//获取参数
	if err := ctx.ShouldBind(&req); err != nil {
		response.ErrorResp(ctx).SetMsg(err.Error()).Log("部门管理", req).WriteJsonExit()
		return
	}
	rows := make([]deptModel.Entity, 0)
	result, err := system.DeptService.SelectListAll(req)

	if err == nil && len(result) > 0 {
		rows = result
	}

	ctx.JSON(http.StatusOK, rows)
}

//新增页面
func (c *deptController) Add(ctx *gin.Context) {
	pid := gconv.Int64(ctx.Query("pid"))

	if pid == 0 {
		pid = 100
	}

	tmp := system.DeptService.SelectDeptById(pid)

	response.BuildTpl(ctx, "system/dept/add").WriteTpl(gin.H{"dept": tmp})
}

//新增页面保存
func (c *deptController) AddSave(ctx *gin.Context) {
	var req *deptModel.AddReq
	//获取参数
	if err := ctx.ShouldBind(&req); err != nil {
		response.ErrorResp(ctx).SetBtype(model.Buniss_Add).SetMsg(err.Error()).Log("部门管理", req).WriteJsonExit()
		return
	}

	if system.DeptService.CheckDeptNameUniqueAll(req.DeptName, req.ParentId) == "1" {
		response.ErrorResp(ctx).SetBtype(model.Buniss_Add).SetMsg("部门名称已存在").Log("部门管理", req).WriteJsonExit()
		return
	}

	rid, err := system.DeptService.AddSave(req, ctx)

	if err != nil || rid <= 0 {
		response.ErrorResp(ctx).SetBtype(model.Buniss_Add).Log("部门管理", req).WriteJsonExit()
		return
	}
	response.SucessResp(ctx).SetBtype(model.Buniss_Add).Log("部门管理", req).WriteJsonExit()
}

//修改页面
func (c *deptController) Edit(ctx *gin.Context) {
	id := gconv.Int64(ctx.Query("id"))
	if id <= 0 {
		response.BuildTpl(ctx, model.ERROR_PAGE).WriteTpl(gin.H{
			"desc": "参数错误",
		})
		return
	}

	dept := system.DeptService.SelectDeptById(id)

	if dept == nil || dept.DeptId <= 0 {
		response.BuildTpl(ctx, model.ERROR_PAGE).WriteTpl(gin.H{
			"desc": "部门不存在",
		})
		return
	}

	response.BuildTpl(ctx, "system/dept/edit").WriteTpl(gin.H{
		"dept": dept,
	})
}

//修改页面保存
func (c *deptController) EditSave(ctx *gin.Context) {
	var req *deptModel.EditReq
	//获取参数
	if err := ctx.ShouldBind(&req); err != nil {
		response.ErrorResp(ctx).SetBtype(model.Buniss_Edit).SetMsg(err.Error()).Log("部门管理", req).WriteJsonExit()
		return
	}

	if system.DeptService.CheckDeptNameUnique(req.DeptName, req.DeptId, req.ParentId) == "1" {
		response.ErrorResp(ctx).SetBtype(model.Buniss_Edit).SetMsg("部门名称已存在").Log("部门管理", req).WriteJsonExit()
		return
	}

	rs, err := system.DeptService.EditSave(req, ctx)

	if err != nil || rs <= 0 {
		response.ErrorResp(ctx).SetBtype(model.Buniss_Edit).Log("部门管理", req).WriteJsonExit()
		return
	}
	response.SucessResp(ctx).SetData(rs).SetBtype(model.Buniss_Edit).Log("部门管理", req).WriteJsonExit()
}

//删除数据
func (c *deptController) Remove(ctx *gin.Context) {
	id := gconv.Int64(ctx.Query("id"))
	rs := system.DeptService.DeleteDeptById(id)

	if rs > 0 {
		response.SucessResp(ctx).SetBtype(model.Buniss_Del).Log("部门管理", gin.H{"id": id}).WriteJsonExit()
	} else {
		response.ErrorResp(ctx).SetBtype(model.Buniss_Del).Log("部门管理", gin.H{"id": id}).WriteJsonExit()
	}
}

//加载部门列表树结构的数据
func (c *deptController) TreeData(ctx *gin.Context) {
	result, _ := system.DeptService.SelectDeptTree(0, "", "")
	ctx.JSON(http.StatusOK, result)
}

//加载部门列表树选择页面
func (c *deptController) SelectDeptTree(ctx *gin.Context) {
	deptId := gconv.Int64(ctx.Query("deptId"))
	deptPoint := system.DeptService.SelectDeptById(deptId)

	if deptPoint != nil {
		response.BuildTpl(ctx, "system/dept/tree").WriteTpl(gin.H{
			"dept": *deptPoint,
		})
	} else {
		response.BuildTpl(ctx, "system/dept/tree").WriteTpl()
	}
}

//加载角色部门（数据权限）列表树
func (c *deptController) RoleDeptTreeData(ctx *gin.Context) {
	roleId := gconv.Int64(ctx.Query("roleId"))
	result, err := system.DeptService.RoleDeptTreeData(roleId)

	if err != nil {
		response.ErrorResp(ctx).SetMsg(err.Error()).Log("菜单树", gin.H{"roleId": roleId})
		return
	}

	ctx.JSON(http.StatusOK, result)
}

//检查部门名称是否已经存在
func (c *deptController) CheckDeptNameUnique(ctx *gin.Context) {
	var req *deptModel.CheckDeptNameReq
	if err := ctx.ShouldBind(&req); err != nil {
		ctx.Writer.WriteString("1")
		return
	}

	result := system.DeptService.CheckDeptNameUnique(req.DeptName, req.DeptId, req.ParentId)

	ctx.Writer.WriteString(result)
}

//检查部门名称是否已经存在
func (c *deptController) CheckDeptNameUniqueAll(ctx *gin.Context) {
	var req *deptModel.CheckDeptNameALLReq
	if err := ctx.ShouldBind(&req); err != nil {
		ctx.Writer.WriteString("1")
		return
	}

	result := system.DeptService.CheckDeptNameUniqueAll(req.DeptName, req.ParentId)

	ctx.Writer.WriteString(result)
}
